package com.example.sqldealtool.dealSqlToWord;

import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public class ToExportTaskId {
    public static void main(String[] args) throws Exception {
        //在getSession修改指定的数据库连接url和密码（任意）
        SqlSessionFactory sqlSessionFactory = getSession();
        SqlSession session = sqlSessionFactory.openSession();
        TableMapper tableMapper = session.getMapper(TableMapper.class);
        String bigJson = tableMapper.getBigJson();
        Gson gson = new Gson();
        Map<String, Object> body = (Map<String, Object>) gson.fromJson(bigJson,Map.class);
        Map<String, Object> data = (Map<String, Object>) gson.fromJson(gson.toJson(body.get("data")),Map.class);
        List<CarSceneColorResult> result = gson.fromJson(gson.toJson(data.get("carSceneColorResults")), List.class);
        if(!CollectionUtils.isEmpty(result)){

            System.out.println("====result====");
            System.out.println(result.size());
        }
    }

    public static SqlSessionFactory getSession() {
        //数据库连接池信息
        PooledDataSource dataSource = new PooledDataSource();
        dataSource.setDriver("com.mysql.cj.jdbc.Driver");
        dataSource.setUsername("root");
        dataSource.setPassword("Arcvideo@01");
        dataSource.setUrl("jdbc:mysql://172.17.20.181:3306/amam?useUnicode=yes&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&character_set_server=utf8mb4&useSSL=false&allowPublicKeyRetrieval=true");
        dataSource.setDefaultAutoCommit(false);
        //采用MyBatis的JDBC事务方式
        TransactionFactory transactionFactory = new JdbcTransactionFactory();
        Environment environment = new Environment("development", transactionFactory,
                dataSource);
        //创建 Configuration对象
        Configuration configuration = new Configuration(environment);
        //注册一个MyBatis上下文别名
        configuration.getTypeAliasRegistry().registerAlias("tableMapper", TableMapper.class);
        //加入一个映射器
        configuration.addMapper(TableMapper.class);
        //使用 SqlSessionFactoryBuilder 构建SqlSessionFactory
        SqlSessionFactory SqlSessionFactory =
                new SqlSessionFactoryBuilder().build(configuration);
        return SqlSessionFactory;
    }

    interface CommonMapper {
        @Select("SELECT sdt.voucher_id from amam.smart_distinguish_task sdt left join amam.file_object_data fod on sdt.file_object_id =fod.id " +
                "where fod.file_type in (5000,8000,8500) and sdt.updated_time BETWEEN '2024-01-15 00:00:00' and '2024-01-15 23:59:59' and fod.smart_distinguish_status =3 ORDER by sdt.updated_time desc")
        List<Long> getSmartPlatformId();

        @Select("SELECT result from eyedropper_task where id =3 ")
        String getBigJson();
    }

}
